<template>
  <div class="login-container flex-column-center">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form flex-column-center"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <div class="title">数字产业大脑运维管理中心</div>
      </div>
      <el-form-item prop="account">
        <el-input
          v-model="loginForm.account"
          placeholder="用户帐号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        >
          <div slot="prefix" class="svg-container flex-column justify-center">
            <div class="flex-column-center">
              <svg-icon icon-class="user" />
            </div>
          </div>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
          @input="handleChangePassword"
        >
          <div slot="prefix" class="svg-container flex-column justify-center">
            <div class="flex-column-center">
              <svg-icon icon-class="password" />
            </div>
          </div>
          <div slot="suffix" class="show-pwd " @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </div>
        </el-input>
      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        class="login-btn"
        @click.native.prevent="handleLogin"
      >
        登 录
      </el-button>
      <!--      <el-button type="primary" @click="testLogin">测试接口</el-button>-->
    </el-form>
  </div>
</template>

<script>
import md5 from 'js-md5'

export default {
  name: 'Login',
  filters: {
    passwordFormat(value) {
      return md5(value)
    },
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        account: 'admin_test',
        password: '1234.abcd',
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', message: '登录帐号不能为空' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  methods: {
    testLogin() {
      this.$store
        .dispatch('user/login', {
          account: 'HunterTest',
          password: '00509c4eee4657da3350647879179e3e',
          requestSourceEnum: this.$consts.MenuTypeEnum.MANEGE,
        })
        .then(() => {
          // getRouters().then(Res => {
          //   console.log(generatedMenu(allRouters, Res.data))
          // })
        })
      // const formData = {
      //   account: 'test040602',
      //   password: '00509c4eee4657da3350647879179e3e'
      // }
      // const url = 'http://192.168.68.114:10088/user/login'
      // this.$axios.post(url, formData).then(res => {
      //   console.log(res)
      // })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // this.loading = true
          this.$store
            .dispatch('user/login', {
              account: this.loginForm.account,
              password: md5(this.loginForm.password),
            })
            .then(() => {
              // window.open('http://127.0.0.1:32767/start.html#id=5mkwbr&p=cloud-platform&c=1')

              // const isBack = this.$route.query.isBack
              // if (isBack) {
              //   window.location.href = 'http://szls.chiefdata.net/cloud-platform.html'
              //   return
              // }
              this.$router.replace({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleChangePassword(value) {
      this.loginForm.password = value
    },
  },
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */

/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #000;

.login-container {
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background: black url('~@/assets/img/login-bgimg.png') no-repeat;
  background-size: 100% 100%;

  .login-form {
    position: relative;
    width: vw(448px);
    height: vw(382px);
    // padding: vw(45px) vw(54px);
    margin: 0 auto;
    overflow: hidden;
    background: white;
    border-radius: 4px;
    box-shadow: -2px -1px 8px 0 rgba(127, 127, 127, 0.91) inset;
    box-sizing: border-box;
  }

  .svg-container {
    // display: inline-block;
    width: vw(48px);
    height: 100%;
    // color: $dark_gray;
    // vertical-align: middle;

    .svg-icon {
      width: vw(18px);
      height: vw(18px);
    }
  }

  ::v-deep .el-input--prefix .el-input__inner {
    padding-left: vw(48px);
  }

  .title-container {
    position: relative;
    margin-bottom: vw(43px);

    .title {
      // margin: 0 auto 40px auto;
      font-size: vw(32px);
      font-weight: bold;
      color: #333;
      text-align: center;
    }
  }

  .login-btn {
    width: vw(343px);
    height: vw(50px);
    font-size: vw(22px);
    letter-spacing: vw(2px);
  }

  .input {
    width: vw(343px);
    height: vw(50px);
    font-size: vw(16px);
  }

  .el-input {
    width: vw(343px);
  }

  ::v-deep .el-input__inner {
    height: vw(50px) !important;
    line-height: vw(50px);
  }

  ::v-deep .el-input__icon {
    height: auto;
    line-height: vw(50px);
  }

  ::v-deep .el-form-item__content {
    line-height: vw(50px);
  }

  ::v-deep .el-form-item {
    margin-bottom: vw(25px);
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
